package cn.cdjsyun.trms;

import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.List;

/**
 * @ProjectName: web-crawler
 * @Package: cn.cdjsyun.trms
 * @ClassName: SlidePuzzleUnlock
 * @Description: Java 类说明
 * @Author: xiaLin
 * @Version: 1.0
 */
public class SlidePuzzleUnlock {
  public static void main(String[] args) throws IOException {

    // 设置 ChromeDriver 路径
    System.setProperty("webdriver.chrome.driver", "D:\\Project\\web-crawler\\chromedriver\\chromedriver.exe");

    // 配置 Chrome 浏览器
    ChromeOptions options = new ChromeOptions();
    // 如果不想显示浏览器界面
//    options.addArguments("--headless");
    // 设置启动时最大化浏览器窗口
    options.addArguments("--start-maximized");
    // 指定浏览器位置
    options.setBinary("D:\\Project\\web-crawler\\chrome-windows\\chrome.exe");
    WebDriver driver = new ChromeDriver(options);
    driver.get("http://182.131.3.111:xxx/noticerpt/user/popup_login");

    // 显式等待，确保登录页面加载完成
    WebDriverWait loginWait = new WebDriverWait(driver, Duration.ofSeconds(30));
    // 获取背景图和拼图块
    // 拼图背景图
    WebElement button = loginWait.until(ExpectedConditions.presenceOfElementLocated(By.className("button4")));
    button.click();
    WebElement automv = loginWait.until(ExpectedConditions.presenceOfElementLocated(By.id("automv_1")));
    WebElement aBtn = automv.findElement(By.tagName("a"));
    aBtn.click();

    // 拼图块
    WebElement nav = driver.findElement(By.id("nav"));
    List<WebElement> elementList = nav.findElements(By.tagName("li"));
    WebElement navLi = elementList.get(1);
    WebElement navLiA = navLi.findElement(By.tagName("a"));
    navLiA.click();

    WebElement smsLoginFormsc = driver.findElement(By.id("smsLoginFormsc"));
    WebElement embedCaptchaSms = smsLoginFormsc.findElement(By.id("embed-captcha-sms"));
    WebElement gtSlider = embedCaptchaSms.findElement(By.className("gt_slider"));
    WebElement gtSliderKnob = gtSlider.findElement(By.className("gt_slider_knob"));
    new Actions(driver).moveToElement(gtSliderKnob).perform();


//    WebElement gtWidget = smsLoginFormsc.findElement(By.className("gt_widget"));
//    WebElement gtWidget = smsLoginFormsc.findElement(By.className("gt_widget"));
//    WebElement gtWidget = smsLoginFormsc.findElement(By.className("gt_widget"));

    // 获取拼图块的位置
//    Point sliderPosition = slider.getLocation();
//    System.out.println("拼图块的初始位置: " + sliderPosition);

//    // 计算缺失的拼图块位置（假设通过图像处理得出：x = 200, y = 0）
//    int targetX = 200;
//    int targetY = 0;
//    String imageUrl = backgroundElement.getAttribute("src");
//    BufferedImage background = ImageIO.read(new URL(imageUrl));
//    imageUrl = puzzlePieceElement.getAttribute("src");
//    BufferedImage puzzlePiece = ImageIO.read(new URL(imageUrl));
//
//    // 比较两张图片的差异
//    int diffX = 0;
//    int diffY = 0;
//
//    for (int y = 0; y < background.getHeight(); y++) {
//      for (int x = 0; x < background.getWidth(); x++) {
//        int bgPixel = background.getRGB(x, y);
//        int puzzlePixel = puzzlePiece.getRGB(x, y);
//
//        if (bgPixel != puzzlePixel) {
//          diffX = x;
//          diffY = y;
//          break;
//        }
//      }
//    }

    // 获取拼图块的当前位置
//    Point start = slider.getLocation();

//    // 模拟滑动拼图块
//    Actions actions = new Actions(driver);
//    actions.clickAndHold(slider)
//        .moveByOffset(targetX - start.getX(), targetY - start.getY())
//        .release()
//        .perform();

    // 等待拼图验证完成
//    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
//    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("success-message")));
//
//    System.out.println("拼图解锁成功！");

    // 关闭浏览器
//    driver.quit();

  }
}
